package zdh.avatar.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import zdh.avatar.base.*;

/**
 *
 * @author Gem Seeker
 */
@SuppressWarnings("serial")
public class AddEditGroup extends JFrame implements ActionListener {

    public static final byte ADD = 0x0;
    public static final byte EDIT = 0x1;
    
    private final MainFrame main;
    private final byte state;
    
    private final ElementPanel oldPanel;
    private ElementPanel newPanel;
    
    private final String elementsPath;

    public AddEditGroup(MainFrame main, String elementsPath, byte state, ElementPanel panel) {
        this.main = main;
        this.elementsPath = elementsPath;
        this.state = state;
        oldPanel = panel;
        
        initComponents();
        setComponents();
        initListeners();
        
        this.getRootPane().setDefaultButton(btn_save);
    }

//    public void addGroup() {
//        state = ADD;
//    }
//
//    public void editGroup(ElementPanel panel) {
//        state = EDIT;
//        tf_name.setText(panel.getPanelName());
//        tf_color.setBackground(panel.getBgColor());
//        oldPanel = panel;
//    }

    private void setComponents(){
        if(state == EDIT){
            SwingUtilities.invokeLater(new Runnable(){
                @Override public void run(){
                    tf_name.setText(oldPanel.getPanelName());
                    tf_color.setBackground(oldPanel.getBgColor());
                }
            });
        }
    }
    
    private void initListeners() {
        btn_choose.addActionListener(this);
        btn_save.addActionListener(this);
        btn_cancel.addActionListener(this);

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
//                editGUI.setEnabled(true);
                main.setEnabled(true);
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (source == btn_choose) {
            Color nCol = JColorChooser.showDialog(this, "Background Color", tf_color.getBackground());
            tf_color.setBackground(nCol);
        } else if (source == btn_save) {
            if (tf_name.getText().equals("")) {
                JOptionPane.showMessageDialog(this, "Button Group name is empty.");
                tf_name.selectAll();
                tf_name.requestFocusInWindow();
            } else {

                if (state == ADD) {
                    int tabIndex;
                    if (main.getPanelCount() == 0) {
                        tabIndex = 1;
                    } else {
                        tabIndex = main.getPanelCount() + 2;
                    }

                    newPanel = new ElementPanel(
                            tf_name.getText(),
                            tf_color.getBackground(),
                            tabIndex);

                    System.out.println(newPanel.toString() + tabIndex);

                    boolean added = ElementLoader.addElementPanel(
                            newPanel,
                            elementsPath + "panels/",
                            false);

                    if (!added) { // means file already exist
                        JOptionPane.showMessageDialog(
                                this, "Button Group already exist, try a new name.");

                        tf_name.selectAll();
                        tf_name.requestFocusInWindow();

                    } else {
                        main.loadButtons();
//                        editGUI.reloadList();
                        closeWindow();
                    }
                } else if (state == EDIT) {

                    newPanel = new ElementPanel(
                            tf_name.getText(),
                            tf_color.getBackground(),
                            oldPanel.getTabIndex());

                    ElementLoader.replaceElementPanel(
                            oldPanel,
                            newPanel,
                            elementsPath);

                    main.loadButtons();
//                    editGUI.reloadList();
                    closeWindow();
                }
            }
        } else if (source == btn_cancel) {
            closeWindow();
        }
    }

    public void showWindow() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
//        		editGUI.setEnabled(false);
                main.setEnabled(false);
                setLocationRelativeTo(main);
                setVisible(true);
            }
        });
    }

    public void closeWindow() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
//        		editGUI.setEnabled(true);
                main.setEnabled(true);
                dispose();
            }
        });
    }

    // <editor-fold defaultstate="collapsed" desc="Generated by NetBeans">
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tf_name = new javax.swing.JTextField();
        tf_color = new javax.swing.JTextField();
        lbl_color = new javax.swing.JLabel();
        lbl_name = new javax.swing.JLabel();
        btn_choose = new javax.swing.JButton();
        btn_cancel = new javax.swing.JButton();
        btn_save = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Group");

        tf_color.setEditable(false);
        tf_color.setBackground(new java.awt.Color(255, 255, 255));

        lbl_color.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lbl_color.setText("Background:");

        lbl_name.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lbl_name.setText("Group Name:");

        btn_choose.setText("Choose Color...");

        btn_cancel.setText("Cancel");

        btn_save.setText("Save");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_name)
                            .addComponent(lbl_color))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(tf_color, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btn_choose))
                            .addComponent(tf_name, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btn_save)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_cancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_name, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_name))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_color, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_color)
                    .addComponent(btn_choose))
                .addGap(7, 7, 7)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_cancel)
                    .addComponent(btn_save))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_cancel;
    private javax.swing.JButton btn_choose;
    private javax.swing.JButton btn_save;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lbl_color;
    private javax.swing.JLabel lbl_name;
    private javax.swing.JTextField tf_color;
    private javax.swing.JTextField tf_name;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>
}
